﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace OpticalPowerMeter
{
    
    /// <summary>
    /// 管理可选波长列表
    /// </summary>
    public class WavelengthListManagement
    {
        private static WavelengthListManagement wavelengthes = null;

        private WavelengthListManagement() { }

        /// <summary>
        /// 单例模式，获取对象的方式
        /// </summary>
        /// <returns>WavelengthListManagement对象</returns>
        public static WavelengthListManagement GetInstance()
        {
            if (wavelengthes == null)
            {
                wavelengthes = new WavelengthListManagement();
                wavelengthes.LoadDefaultWavelength();  //载入默认波长列表
            }
            return wavelengthes;
        }


        //配置文件保存的完整名称
        private string FileName = System.Environment.CurrentDirectory + "\\Config\\" + "Wavelengthes.xml";

        private void InitFile()
        {
            string path = System.Environment.CurrentDirectory + "\\Config";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            #region 如果文件不存在，添加文件
            if (!File.Exists(FileName))
            {
                XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement("Wavelengthes"));
                xDoc.Save(FileName);
            }
            #endregion
        }

        /// <summary>
        /// 保存波长清单
        /// </summary>
        /// <param name="wavelengthList">波长清单</param>
        public void SaveData(List<MyWavelength> paramWavelengthList) {
            InitFile();
            XElement root = XElement.Load(FileName);
            paramWavelengthList.Sort(); //排序波长列表
            List<MyWavelength> localWavelengthList = GetWavelengthList();
            bool hasDifferent = false;
            if(paramWavelengthList.Count != localWavelengthList.Count)
            {
                hasDifferent = true;
            }
            for (int i=0; (!hasDifferent) && i<paramWavelengthList.Count;i++) {
                if (!paramWavelengthList[i].Equals(localWavelengthList[i]))
                {
                    hasDifferent = true;
                }
            }
            #region 只要存在差异，直接清除本地清单，重写
            if (hasDifferent) {
                root.RemoveNodes();
                foreach (MyWavelength myWavelength in paramWavelengthList) {
                    XElement waveElement = new XElement("MyWavelength",
                        new XAttribute("Wavelength",myWavelength.Wavelength),
                        new XAttribute("WavelengthMode",myWavelength.Mode));
                    root.Add(waveElement);
                }
                root.Save(FileName);
            }
            #endregion
        }

        /// <summary>
        /// 获取本地波长清单
        /// </summary>
        /// <returns>所有的波长清单</returns>
        public List<MyWavelength> GetWavelengthList()
        {
            List<MyWavelength> myWavelengths = new List<MyWavelength>();
            if (File.Exists(FileName)) {
                XElement root = XElement.Load(FileName);
                IEnumerable<XElement> wavelengthElements = root.Elements("MyWavelength");
                foreach (XElement wavelengthElement in wavelengthElements) {
                    int wavelength = Convert.ToInt32(wavelengthElement.Attribute("Wavelength").Value);
                    WavelengthMode mode = (WavelengthMode)Enum.Parse(typeof(WavelengthMode),wavelengthElement.Attribute("WavelengthMode").Value);
                    MyWavelength myWavelength = new MyWavelength(wavelength,mode);
                    myWavelengths.Add(myWavelength);
                }
            }
            return myWavelengths;
        }

        /// <summary>
        /// 获取本地指定模式（单模或多模）的波长列表
        /// </summary>
        /// <param name="mode">波长模式(单模或多模)</param>
        /// <returns>指定模式的波长列表</returns>
        public List<MyWavelength> GetWavelengthList(WavelengthMode mode) {
            List<MyWavelength> allWavelengthes = GetWavelengthList();
            List<MyWavelength> modeOfWavelegthes = new List<MyWavelength>();
            foreach (MyWavelength myWavelength in allWavelengthes) {
                if(myWavelength.Mode == mode)
                {
                    modeOfWavelegthes.Add(myWavelength);
                }
            }
            return modeOfWavelegthes;
        }

        /// <summary>
        /// 设置默认波长，当本地没有配置文件时写入默认波长清单
        /// 850、980、1300、1310、1490、1550、1625、1650
        /// </summary>
        private void LoadDefaultWavelength() {
            if (!File.Exists(FileName))
            {
                List<MyWavelength> myWavelengths = new List<MyWavelength>{
                    new MyWavelength(850,WavelengthMode.Multi),
                    new MyWavelength(980,WavelengthMode.Multi),
                    new MyWavelength(1300,WavelengthMode.Multi),
                    new MyWavelength(1310,WavelengthMode.Single),
                    new MyWavelength(1490,WavelengthMode.Single),
                    new MyWavelength(1550,WavelengthMode.Single),
                    new MyWavelength(1625,WavelengthMode.Single),
                    new MyWavelength(1650,WavelengthMode.Single)
                };
                SaveData(myWavelengths);
            }
        }
    }
}
